#include "sdk.h"
#include "projectsfilemasksdlg.h"

BEGIN_EVENT_TABLE( ProjectsFileMasksDlg, wxScrollingDialog )
  EVT_UPDATE_UI( -1, ProjectsFileMasksDlg::OnUpdateUI )
  EVT_BUTTON( XRCID( "btnAdd" ), ProjectsFileMasksDlg::OnAdd )
  EVT_BUTTON( XRCID( "btnEdit" ), ProjectsFileMasksDlg::OnEdit )
  EVT_BUTTON( XRCID( "btnDelete" ), ProjectsFileMasksDlg::OnDelete )
  EVT_BUTTON( XRCID( "btnSetDefault" ), ProjectsFileMasksDlg::OnSetDefault )
  EVT_LISTBOX( XRCID( "lstCategories" ), ProjectsFileMasksDlg::OnListChanged )
END_EVENT_TABLE()

ProjectsFileMasksDlg::ProjectsFileMasksDlg( wxWindow* parent, FilesGroupsAndMasks* fgam ) : m_FileGroupsAndMasksCopy( *fgam ), m_pFileGroupsAndMasks( fgam ), m_LastListSelection( 0 ) {
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/配置项目类别.xrc" );
  if( !xml_res->Load( resPath ) )  {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( F( _T( "文件丢失: %s " ), resPath.wx_str() ) );
    return;
  }
  if( !xml_res->LoadObject( this, parent, _T( "dlgProjectsFileMasks" ), _T( "wxScrollingDialog" ) ) ) {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( _T( "资源丢失" ) );
    return;
  }
  xml_File_lock = true;
  XRCCTRL( *this, "wxID_OK", wxButton )->SetDefault();
  RebuildList();
}

ProjectsFileMasksDlg::~ProjectsFileMasksDlg() {
}

void ProjectsFileMasksDlg::RebuildList() {
  wxListBox* pList = XRCCTRL( *this, "lstCategories", wxListBox );
  pList->Clear();
  for( unsigned int i = 0; i < m_FileGroupsAndMasksCopy.GetGroupsCount(); ++i ) {
    pList->Append( m_FileGroupsAndMasksCopy.GetGroupName( i ) );
  }
  if( pList->GetCount() != 0 ) {
    pList->SetSelection( m_LastListSelection );
    ListChange();
  }
}

void ProjectsFileMasksDlg::ListChange() {
  wxTextCtrl* pText = XRCCTRL( *this, "txtFileMasks", wxTextCtrl );
  int sel = XRCCTRL( *this, "lstCategories", wxListBox )->GetSelection();
  if( sel != m_LastListSelection ) {
    if( pText->GetValue() != m_FileGroupsAndMasksCopy.GetFileMasks( m_LastListSelection ) ) {
      m_FileGroupsAndMasksCopy.SetFileMasks( m_LastListSelection, pText->GetValue() );
    }
  }
  pText->SetValue( m_FileGroupsAndMasksCopy.GetFileMasks( sel ) );
  m_LastListSelection = sel;
}

void ProjectsFileMasksDlg::OnUpdateUI( M_Code_unused wxUpdateUIEvent& event ) {
  int sel = XRCCTRL( *this, "lstCategories", wxListBox )->GetSelection();
  XRCCTRL( *this, "btnEdit", wxButton )->Enable( sel >= 0 );
  XRCCTRL( *this, "btnDelete", wxButton )->Enable( sel >= 0 );
}

void ProjectsFileMasksDlg::OnAdd( M_Code_unused wxCommandEvent& event ) {
  wxString groupName = cbGetTextFromUser( _T( "输入新组名:" ), _T( "新分组" ) );
  if( groupName.IsEmpty() ) {
    return;
  }
  m_FileGroupsAndMasksCopy.AddGroup( groupName );
  wxListBox* pList = XRCCTRL( *this, "lstCategories", wxListBox );
  pList->Append( groupName );
  pList->SetSelection( pList->GetCount() - 1 );
  ListChange();
  XRCCTRL( *this, "txtFileMasks", wxTextCtrl )->SetFocus();
}

void ProjectsFileMasksDlg::OnEdit( M_Code_unused wxCommandEvent& event ) {
  wxListBox* pList = XRCCTRL( *this, "lstCategories", wxListBox );
  wxString oldName = pList->GetStringSelection();
  wxString groupName = cbGetTextFromUser( _T( "重命名组:" ), _T( "编辑组" ), oldName );
  if( !groupName.IsEmpty() && groupName != oldName ) {
    m_FileGroupsAndMasksCopy.RenameGroup( pList->GetSelection(), groupName );
    pList->SetString( pList->GetSelection(), groupName );
  }
}

void ProjectsFileMasksDlg::OnDelete( M_Code_unused wxCommandEvent& event ) {
  wxListBox* pList = XRCCTRL( *this, "lstCategories", wxListBox );
  wxString name = pList->GetStringSelection();
  wxString caption;
  caption.Printf( _T( "是否确实要删除组 \"%s\"?" ), name.c_str() );
  if( msging( caption, _T( "审查" ), wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION, this ) == wxID_NO ) {
    return;
  }
  m_FileGroupsAndMasksCopy.DeleteGroup( pList->GetSelection() );
  RebuildList();
}

void ProjectsFileMasksDlg::OnSetDefault( M_Code_unused wxCommandEvent& event ) {
  m_FileGroupsAndMasksCopy.SetDefault();
  RebuildList();
}

void ProjectsFileMasksDlg::OnListChanged( M_Code_unused wxCommandEvent& event ) {
  ListChange();
}

void ProjectsFileMasksDlg::EndModal( int retCode ) {
  if( retCode == wxID_OK ) {
    wxTextCtrl* pText = XRCCTRL( *this, "txtFileMasks", wxTextCtrl );
    if( pText->GetValue() != m_FileGroupsAndMasksCopy.GetFileMasks( m_LastListSelection ) ) {
      m_FileGroupsAndMasksCopy.SetFileMasks( m_LastListSelection, pText->GetValue() );
    }
    m_pFileGroupsAndMasks->CopyFrom( m_FileGroupsAndMasksCopy );
  }
  wxScrollingDialog::EndModal( retCode );
}
